package qj.srt;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import qj.ui.DesktopUI4;
import qj.util.DateUtil;
import qj.util.IOUtil;
import qj.util.RegexUtil;
import qj.util.funct.F0;
import qj.util.funct.P1;

public class Srt {
	public long from;
	public long to;
	public String text;
	public Srt(long from, long to, String text) {
		this.from = from;
		this.to = to;
		this.text = text;
	}
	public Srt back(long separate) {
		from -= separate;
		to -= separate;
		return this;
	}

	static SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss,SSS");
	public String toString(int i) {
		return 
		i + "\n" +
		df.format(new Date(from)) + " --> " + df.format(new Date(to)) + "\n" +
		text + "\n" + 
		"\n"
		;
	}
	
	public static F0<Srt> reader(InputStream in) {
		final Pattern ptn = RegexUtil.compileF.e("\\d+\r?\n(\\S+) --> (\\S+)\r?\n((?:.|\r?\n)*?)\r?\n\r?\n");
		final SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss,SSS");
		
		try {
			in.read();
			in.read();
			in.read();
			final Scanner scanner = new Scanner(new InputStreamReader(in, "UTF-8"));

			return new F0<Srt>() {public Srt e() {
				String str = scanner.findWithinHorizon(ptn, 0);
				if (str == null) {
					return null;
				}
				Matcher m = ptn.matcher(str);
				m.matches();
				return new Srt(
						DateUtil.parse(m.group(1), df).getTime(),
						DateUtil.parse(m.group(2), df).getTime(),
						m.group(3)
				);
			}};
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	public static void readSrt(InputStream in, P1<Srt> p1) {
		Pattern ptn = RegexUtil.compileF.e("\\d+\r?\n(\\S+) --> (\\S+)\r?\n((?:.|\r?\n)*?)\r?\n\r?\n");
		SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss,SSS");
		
		try {
			in.read();
			in.read();
//			in.read();
//			System.out.println(Arrays.toString(IOUtil.readEnough(3, in)));
//			System.out.println(new String(IOUtil.readEnough(300, in)).replaceAll("\r", ""));
//			DesktopUI4.alert2(IOUtil.readAll(new InputStreamReader(in
//					, "UTF-16LE"
//					)));
			Scanner scanner = new Scanner(new InputStreamReader(in
					, "UTF-16LE"
					));
	
			for (String str; (str = scanner.findWithinHorizon(ptn, 0)) != null;) {
				Matcher m = ptn.matcher(str);
				m.matches();
				p1.e(new Srt(
						DateUtil.parse(m.group(1), df).getTime(),
						DateUtil.parse(m.group(2), df).getTime(),
						m.group(3)
				));
			}
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static P1<Srt> writer(final OutputStream out) {
		final int[] i = {0};
		return new P1<Srt>() {public void e(Srt srt) {
			try {
				out.write(srt.toString(++i[0]).getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e1) {
				throw new RuntimeException(e1);
			} catch (IOException e1) {
				throw new RuntimeException(e1);
			}
		}};
	}
}
